home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJTST200.ZIP / tests / libc / ansi / math / paranoia / paranoia.c < prev   
Encoding:
C/C++ Source or Header  |  1995-06-01  |  60.1 KB  |  2,258 lines

  1. /*    A C version of Kahan's Floating Point Test "Paranoia"
  2.  
  3.             Thos Sumner, UCSF, Feb. 1985
  4.             David Gay, BTL, Jan. 1986
  5.  
  6.     This is a rewrite from the Pascal version by
  7.  
  8.             B. A. Wichmann, 18 Jan. 1985
  9.  
  10.     (and does NOT exhibit good C programming style).
  11.  
  12.     Adjusted to use Standard C headers 19 Jan. 1992 (dmg);
  13.     compile with -DKR_headers or insert
  14. #define KR_headers
  15.     at the beginning if you have an old-style C compiler.
  16.  
  17. (C) Apr 19 1983 in BASIC version by:
  18.     Professor W. M. Kahan,
  19.     567 Evans Hall
  20.     Electrical Engineering & Computer Science Dept.
  21.     University of California
  22.     Berkeley, California 94720
  23.     USA
  24.  
  25. converted to Pascal by:
  26.     B. A. Wichmann
  27.     National Physical Laboratory
  28.     Teddington Middx
  29.     TW11 OLW
  30.     UK
  31.  
  32. converted to C by:
  33.  
  34.     David M. Gay        and    Thos Sumner
  35.     AT&T Bell Labs            Computer Center, Rm. U-76
  36.     600 Mountain Avenue        University of California
  37.     Murray Hill, NJ 07974        San Francisco, CA 94143
  38.     USA                USA
  39.  
  40. with simultaneous corrections to the Pascal source (reflected
  41. in the Pascal source available over netlib).
  42. [A couple of bug fixes from dgh = sun!dhough incorporated 31 July 1986.]
  43.  
  44. Reports of results on various systems from all the versions
  45. of Paranoia are being collected by Richard Karpinski at the
  46. same address as Thos Sumner.  This includes sample outputs,
  47. bug reports, and criticisms.
  48.  
  49. You may copy this program freely if you acknowledge its source.
  50. Comments on the Pascal version to NPL, please.
  51.  
  52.  
  53. The C version catches signals from floating-point exceptions.
  54. If signal(SIGFPE,...) is unavailable in your environment, you may
  55. #define NOSIGNAL to comment out the invocations of signal.
  56.  
  57. This source file is too big for some C compilers, but may be split
  58. into pieces.  Comments containing "SPLIT" suggest convenient places
  59. for this splitting.  At the end of these comments is an "ed script"
  60. (for the UNIX(tm) editor ed) that will do this splitting.
  61.  
  62. By #defining Single when you compile this source, you may obtain
  63. a single-precision C version of Paranoia.
  64.  
  65.  
  66. The following is from the introductory commentary from Wichmann's work:
  67.  
  68. The BASIC program of Kahan is written in Microsoft BASIC using many
  69. facilities which have no exact analogy in Pascal.  The Pascal
  70. version below cannot therefore be exactly the same.  Rather than be
  71. a minimal transcription of the BASIC program, the Pascal coding
  72. follows the conventional style of block-structured languages.  Hence
  73. the Pascal version could be useful in producing versions in other
  74. structured languages.
  75.  
  76. Rather than use identifiers of minimal length (which therefore have
  77. little mnemonic significance), the Pascal version uses meaningful
  78. identifiers as follows [Note: A few changes have been made for C]:
  79.  
  80.  
  81. BASIC   C               BASIC   C               BASIC   C               
  82.  
  83.    A                       J                       S    StickyBit
  84.    A1   AInverse           J0   NoErrors           T
  85.    B    Radix                    [Failure]         T0   Underflow
  86.    B1   BInverse           J1   NoErrors           T2   ThirtyTwo
  87.    B2   RadixD2                  [SeriousDefect]   T5   OneAndHalf
  88.    B9   BMinusU2           J2   NoErrors           T7   TwentySeven
  89.    C                             [Defect]          T8   TwoForty
  90.    C1   CInverse           J3   NoErrors           U    OneUlp
  91.    D                             [Flaw]            U0   UnderflowThreshold
  92.    D4   FourD              K    PageNo             U1
  93.    E0                      L    Milestone          U2
  94.    E1                      M                       V
  95.    E2   Exp2               N                       V0
  96.    E3                      N1                      V8
  97.    E5   MinSqEr            O    Zero               V9
  98.    E6   SqEr               O1   One                W
  99.    E7   MaxSqEr            O2   Two                X
  100.    E8                      O3   Three              X1
  101.    E9                      O4   Four               X8
  102.    F1   MinusOne           O5   Five               X9   Random1
  103.    F2   Half               O8   Eight              Y
  104.    F3   Third              O9   Nine               Y1
  105.    F6                      P    Precision          Y2
  106.    F9                      Q                       Y9   Random2
  107.    G1   GMult              Q8                      Z
  108.    G2   GDiv               Q9                      Z0   PseudoZero
  109.    G3   GAddSub            R                       Z1
  110.    H                       R1   RMult              Z2
  111.    H1   HInverse           R2   RDiv               Z9
  112.    I                       R3   RAddSub
  113.    IO   NoTrials           R4   RSqrt
  114.    I3   IEEE               R9   Random9
  115.  
  116.    SqRWrng
  117.  
  118. All the variables in BASIC are true variables and in consequence,
  119. the program is more difficult to follow since the "constants" must
  120. be determined (the glossary is very helpful).  The Pascal version
  121. uses Real constants, but checks are added to ensure that the values
  122. are correctly converted by the compiler.
  123.  
  124. The major textual change to the Pascal version apart from the
  125. identifiersis that named procedures are used, inserting parameters
  126. wherehelpful.  New procedures are also introduced.  The
  127. correspondence is as follows:
  128.  
  129.  
  130. BASIC       Pascal
  131. lines 
  132.  
  133.   90- 140   Pause
  134.  170- 250   Instructions
  135.  380- 460   Heading
  136.  480- 670   Characteristics
  137.  690- 870   History
  138. 2940-2950   Random
  139. 3710-3740   NewD
  140. 4040-4080   DoesYequalX
  141. 4090-4110   PrintIfNPositive
  142. 4640-4850   TestPartialUnderflow
  143.  
  144. =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  145.  
  146. Below is an "ed script" that splits para.c into 10 files
  147. of the form part[1-8].c, subs.c, and msgs.c, plus a header
  148. file, paranoia.h, that these files require.
  149.  
  150. r paranoia.c
  151. $
  152. ?SPLIT
  153.  .d
  154. +d
  155. -,$w msgs.c
  156. -,$d
  157. ?SPLIT
  158.  .d
  159. +d
  160. -,$w subs.c
  161. -,$d
  162. ?part8
  163. +d
  164. ?include
  165.  .,$w part8.c
  166.  .,$d
  167. -d
  168. ?part7
  169. +d
  170. ?include
  171.  .,$w part7.c
  172.  .,$d
  173. -d
  174. ?part6
  175. +d
  176. ?include
  177.  .,$w part6.c
  178.  .,$d
  179. -d
  180. ?part5
  181. +d
  182. ?include
  183.  .,$w part5.c
  184.  .,$d
  185. -d
  186. ?part4
  187. +d
  188. ?include
  189.  .,$w part4.c
  190.  .,$d
  191. -d
  192. ?part3
  193. +d
  194. ?include
  195.  .,$w part3.c
  196.  .,$d
  197. -d
  198. ?part2
  199. +d
  200. ?include
  201.  .,$w part2.c
  202.  .,$d
  203. ?SPLIT
  204.  .d
  205. 1,/^#include/-1d
  206. 1,$w part1.c
  207. /Computed constants/,$d
  208. 1,$s/^int/extern &/
  209. 1,$s/^FLOAT/extern &/
  210. 1,$s/^char/extern &/
  211. 1,$s! = .*!;!
  212. /^Guard/,/^Round/s/^/extern /
  213. /^jmp_buf/s/^/extern /
  214. /^Sig_type/s/^/extern /
  215. s/$/\
  216. extern void sigfpe(INT);/
  217. w paranoia.h
  218. q
  219.  
  220. */
  221.  
  222. #include <stdio.h>
  223. #ifndef NOSIGNAL
  224. #include <signal.h>
  225. #endif
  226. #include <setjmp.h>
  227.  
  228. #ifdef Single
  229. #define FLOAT float
  230. #define FABS(x) (float)fabs((double)(x))
  231. #define FLOOR(x) (float)floor((double)(x))
  232. #define LOG(x) (float)log((double)(x))
  233. #define POW(x,y) (float)pow((double)(x),(double)(y))
  234. #define SQRT(x) (float)sqrt((double)(x))
  235. #else
  236. #define FLOAT double
  237. #define FABS(x) fabs(x)
  238. #define FLOOR(x) floor(x)
  239. #define LOG(x) log(x)
  240. #define POW(x,y) pow(x,y)
  241. #define SQRT(x) sqrt(x)
  242. #endif
  243.  
  244. jmp_buf ovfl_buf;
  245. #ifdef KR_headers
  246. #define VOID /* void */
  247. #define INT /* int */
  248. #define FP /* FLOAT */
  249. #define CHARP /* char * */
  250. #define CHARPP /* char ** */
  251. extern double fabs(), floor(), log(), pow(), sqrt();
  252. extern void exit();
  253. typedef void (*Sig_type)();
  254. FLOAT Sign(), Random();
  255. extern void BadCond();
  256. extern void SqXMinX();
  257. extern void TstCond();
  258. extern void notify();
  259. extern int read();
  260. #else
  261. #define VOID void
  262. #define INT int
  263. #define FP FLOAT
  264. #define CHARP char *
  265. #define CHARPP char **
  266. #ifdef __STDC__
  267. #include <stdlib.h>
  268. #include <math.h>
  269. #else
  270. #ifdef __cplusplus
  271. extern "C" {
  272. #endif
  273. extern double fabs(double), floor(double), log(double);
  274. extern double pow(double,double), sqrt(double);
  275. extern void exit(INT);
  276. #ifdef __cplusplus
  277.     }
  278. #endif
  279. #endif
  280. typedef void (*Sig_type)(int);
  281. FLOAT Sign(FLOAT), Random(void);
  282. extern void BadCond(int, char*);
  283. extern void SqXMinX(int);
  284. extern void TstCond(int, int, char*);
  285. extern void notify(char*);
  286. extern int read(int, char*, int);
  287. #endif
  288. #undef V9
  289. extern void Characteristics(VOID);
  290. extern void Heading(VOID);
  291. extern void History(VOID);
  292. extern void Instructions(VOID);
  293. extern void IsYeqX(VOID);
  294. extern void NewD(VOID);
  295. extern void Pause(VOID);
  296. extern void PrintIfNPositive(VOID);
  297. extern void SR3750(VOID);
  298. extern void SR3980(VOID);
  299. extern void TstPtUf(VOID);
  300.  
  301. Sig_type sigsave;
  302.  
  303. #define KEYBOARD 0
  304.  
  305. FLOAT Radix, BInvrse, RadixD2, BMinusU2;
  306.  
  307. /*Small floating point constants.*/
  308. FLOAT Zero = 0.0;
  309. FLOAT Half = 0.5;
  310. FLOAT One = 1.0;
  311. FLOAT Two = 2.0;
  312. FLOAT Three = 3.0;
  313. FLOAT Four = 4.0;
  314. FLOAT Five = 5.0;
  315. FLOAT Eight = 8.0;
  316. FLOAT Nine = 9.0;
  317. FLOAT TwentySeven = 27.0;
  318. FLOAT ThirtyTwo = 32.0;
  319. FLOAT TwoForty = 240.0;
  320. FLOAT MinusOne = -1.0;
  321. FLOAT OneAndHalf = 1.5;
  322. /*Integer constants*/
  323. int NoTrials = 20; /*Number of tests for commutativity. */
  324. #define False 0
  325. #define True 1
  326.  
  327. /* Definitions for declared types 
  328.     Guard == (Yes, No);
  329.     Rounding == (Chopped, Rounded, Other);
  330.     Message == packed array [1..40] of char;
  331.     Class == (Flaw, Defect, Serious, Failure);
  332.       */
  333. #define Yes 1
  334. #define No  0
  335. #define Chopped 2
  336. #define Rounded 1
  337. #define Other   0
  338. #define Flaw    3
  339. #define Defect  2
  340. #define Serious 1
  341. #define Failure 0
  342. typedef int Guard, Rounding, Class;
  343. typedef char Message;
  344.  
  345. /* Declarations of Variables */
  346. int Indx;
  347. char ch[8];
  348. FLOAT AInvrse, A1;
  349. FLOAT C, CInvrse;
  350. FLOAT D, FourD;
  351. FLOAT E0, E1, Exp2, E3, MinSqEr;
  352. FLOAT SqEr, MaxSqEr, E9;
  353. FLOAT Third;
  354. FLOAT F6, F9;
  355. FLOAT H, HInvrse;
  356. int I;
  357. FLOAT StickyBit, J;
  358. FLOAT MyZero;
  359. FLOAT Precision;
  360. FLOAT Q, Q9;
  361. FLOAT R, Random9;
  362. FLOAT T, Underflow, S;
  363. FLOAT OneUlp, UfThold, U1, U2;
  364. FLOAT V, V0, V9;
  365. FLOAT W;
  366. FLOAT X, X1, X2, X8, Random1;
  367. FLOAT Y, Y1, Y2, Random2;
  368. FLOAT Z, PseudoZero, Z1, Z2, Z9;
  369. int ErrCnt[4];
  370. int fpecount;
  371. int Milestone;
  372. int PageNo;
  373. int M, N, N1;
  374. Guard GMult, GDiv, GAddSub;
  375. Rounding RMult, RDiv, RAddSub, RSqrt;
  376. int Break, Done, NotMonot, Monot, Anomaly, IEEE,
  377.         SqRWrng, UfNGrad;
  378. /* Computed constants. */
  379. /*U1  gap below 1.0, i.e, 1.0-U1 is next number below 1.0 */
  380. /*U2  gap above 1.0, i.e, 1.0+U2 is next number above 1.0 */
  381.  
  382. /* floating point exception receiver */
  383.  void
  384. sigfpe(INT x)
  385. {
  386.     fpecount++;
  387.     printf("\n* * * FLOATING-POINT ERROR %d * * *\n", x);
  388.     fflush(stdout);
  389.     if (sigsave) {
  390. #ifndef NOSIGNAL
  391.         signal(SIGFPE, sigsave);
  392. #endif
  393.         sigsave = 0;
  394.         longjmp(ovfl_buf, 1);
  395.         }
  396.     exit(1);
  397. }
  398.  
  399. int
  400. main(VOID)
  401. {
  402.     /* First two assignments use integer right-hand sides. */
  403.     Zero = 0;
  404.     One = 1;
  405.     Two = One + One;
  406.     Three = Two + One;
  407.     Four = Three + One;
  408.     Five = Four + One;
  409.     Eight = Four + Four;
  410.     Nine = Three * Three;
  411.     TwentySeven = Nine * Three;
  412.     ThirtyTwo = Four * Eight;
  413.     TwoForty = Four * Five * Three * Four;
  414.     MinusOne = -One;
  415.     Half = One / Two;
  416.     OneAndHalf = One + Half;
  417.     ErrCnt[Failure] = 0;
  418.     ErrCnt[Serious] = 0;
  419.     ErrCnt[Defect] = 0;
  420.     ErrCnt[Flaw] = 0;
  421.     PageNo = 1;
  422.     /*=============================================*/
  423.     Milestone = 0;
  424.     /*=============================================*/
  425. #ifndef NOSIGNAL
  426.     signal(SIGFPE, sigfpe);
  427. #endif
  428.     Instructions();
  429.     Pause();
  430.     Heading();
  431.     Pause();
  432.     Characteristics();
  433.     Pause();
  434.     History();
  435.     Pause();
  436.     /*=============================================*/
  437.     Milestone = 7;
  438.     /*=============================================*/
  439.     printf("Program is now RUNNING tests on small integers:\n");
  440.     
  441.     TstCond (Failure, (Zero + Zero == Zero) && (One - One == Zero)
  442.            && (One > Zero) && (One + One == Two),
  443.             "0+0 != 0, 1-1 != 0, 1 <= 0, or 1+1 != 2");
  444.     Z = - Zero;
  445.     if (Z != 0.0) {
  446.         ErrCnt[Failure] = ErrCnt[Failure] + 1;
  447.         printf("Comparison alleges that -0.0 is Non-zero!\n");
  448.         U2 = 0.001;
  449.         Radix = 1;
  450.         TstPtUf();
  451.         }
  452.     TstCond (Failure, (Three == Two + One) && (Four == Three + One)
  453.            && (Four + Two * (- Two) == Zero)
  454.            && (Four - Three - One == Zero),
  455.            "3 != 2+1, 4 != 3+1, 4+2*(-2) != 0, or 4-3-1 != 0");
  456.     TstCond (Failure, (MinusOne == (0 - One))
  457.            && (MinusOne + One == Zero ) && (One + MinusOne == Zero)
  458.            && (MinusOne + FABS(One) == Zero)
  459.            && (MinusOne + MinusOne * MinusOne == Zero),
  460.            "-1+1 != 0, (-1)+abs(1) != 0, or -1+(-1)*(-1) != 0");
  461.     TstCond (Failure, Half + MinusOne + Half == Zero,
  462.           "1/2 + (-1) + 1/2 != 0");
  463.     /*=============================================*/
  464.     /*SPLIT
  465.     {
  466.         extern void part2(VOID), part3(VOID), part4(VOID),
  467.             part5(VOID), part6(VOID), part7(VOID);
  468.         int part8(VOID);
  469.  
  470.         part2();
  471.         part3();
  472.         part4();
  473.         part5();
  474.         part6();
  475.         part7();
  476.         return part8();
  477.         }
  478.     }
  479. #include "paranoia.h"
  480. void part2(VOID){
  481. */
  482.     Milestone = 10;
  483.     /*=============================================*/
  484.     TstCond (Failure, (Nine == Three * Three)
  485.            && (TwentySeven == Nine * Three) && (Eight == Four + Four)
  486.            && (ThirtyTwo == Eight * Four)
  487.            && (ThirtyTwo - TwentySeven - Four - One == Zero),
  488.            "9 != 3*3, 27 != 9*3, 32 != 8*4, or 32-27-4-1 != 0");
  489.     TstCond (Failure, (Five == Four + One) &&
  490.             (TwoForty == Four * Five * Three * Four)
  491.            && (TwoForty / Three - Four * Four * Five == Zero)
  492.            && ( TwoForty / Four - Five * Three * Four == Zero)
  493.            && ( TwoForty / Five - Four * Three * Four == Zero),
  494.           "5 != 4+1, 240/3 != 80, 240/4 != 60, or 240/5 != 48");
  495.     if (ErrCnt[Failure] == 0) {
  496.         printf("-1, 0, 1/2, 1, 2, 3, 4, 5, 9, 27, 32 & 240 are O.K.\n");
  497.         printf("\n");
  498.         }
  499.     printf("Searching for Radix and Precision.\n");
  500.     W = One;
  501.     do  {
  502.         W = W + W;
  503.         Y = W + One;
  504.         Z = Y - W;
  505.         Y = Z - One;
  506.         } while (MinusOne + FABS(Y) < Zero);
  507.     /*.. now W is just big enough that |((W+1)-W)-1| >= 1 ...*/
  508.     Precision = Zero;
  509.     Y = One;
  510.     do  {
  511.         Radix = W + Y;
  512.         Y = Y + Y;
  513.         Radix = Radix - W;
  514.         } while ( Radix == Zero);
  515.     if (Radix < Two) Radix = One;
  516.     printf("Radix = %f .\n", Radix);
  517.     if (Radix != 1) {
  518.         W = One;
  519.         do  {
  520.             Precision = Precision + One;
  521.             W = W * Radix;
  522.             Y = W + One;
  523.             } while ((Y - W) == One);
  524.         }
  525.     /*... now W == Radix^Precision is barely too big to satisfy (W+1)-W == 1
  526.                                           ...*/
  527.     U1 = One / W;
  528.     U2 = Radix * U1;
  529.     printf("Closest relative separation found is U1 = %.7e .\n\n", U1);
  530.     printf("Recalculating radix and precision\n ");
  531.     
  532.     /*save old values*/
  533.     E0 = Radix;
  534.     E1 = U1;
  535.     E9 = U2;
  536.     E3 = Precision;
  537.     
  538.     X = Four / Three;
  539.     Third = X - One;
  540.     F6 = Half - Third;
  541.     X = F6 + F6;
  542.     X = FABS(X - Third);
  543.     if (X < U2) X = U2;
  544.     
  545.     /*... now X = (unknown no.) ulps of 1+...*/
  546.     do  {
  547.         U2 = X;
  548.         Y = Half * U2 + ThirtyTwo * U2 * U2;
  549.         Y = One + Y;
  550.         X = Y - One;
  551.         } while ( ! ((U2 <= X) || (X <= Zero)));
  552.     
  553.     /*... now U2 == 1 ulp of 1 + ... */
  554.     X = Two / Three;
  555.     F6 = X - Half;
  556.     Third = F6 + F6;
  557.     X = Third - Half;
  558.     X = FABS(X + F6);
  559.     if (X < U1) X = U1;
  560.     
  561.     /*... now  X == (unknown no.) ulps of 1 -... */
  562.     do  {
  563.         U1 = X;
  564.         Y = Half * U1 + ThirtyTwo * U1 * U1;
  565.         Y = Half - Y;
  566.         X = Half + Y;
  567.         Y = Half - X;
  568.         X = Half + Y;
  569.         } while ( ! ((U1 <= X) || (X <= Zero)));
  570.     /*... now U1 == 1 ulp of 1 - ... */
  571.     if (U1 == E1) printf("confirms closest relative separation U1 .\n");
  572.     else printf("gets better closest relative separation U1 = %.7e .\n", U1);
  573.     W = One / U1;
  574.     F9 = (Half - U1) + Half;
  575.     Radix = FLOOR(0.01 + U2 / U1);
  576.     if (Radix == E0) printf("Radix confirmed.\n");
  577.     else printf("MYSTERY: recalculated Radix = %.7e .\n", Radix);
  578.     TstCond (Defect, Radix <= Eight + Eight,
  579.            "Radix is too big: roundoff problems");
  580.     TstCond (Flaw, (Radix == Two) || (Radix == 10)
  581.            || (Radix == One), "Radix is not as good as 2 or 10");
  582.     /*=============================================*/
  583.     Milestone = 20;
  584.     /*=============================================*/
  585.     TstCond (Failure, F9 - Half < Half,
  586.            "(1-U1)-1/2 < 1/2 is FALSE, prog. fails?");
  587.     X = F9;
  588.     I = 1;
  589.     Y = X - Half;
  590.     Z = Y - Half;
  591.     TstCond (Failure, (X != One)
  592.            || (Z == Zero), "Comparison is fuzzy,X=1 but X-1/2-1/2 != 0");
  593.     X = One + U2;
  594.     I = 0;
  595.     /*=============================================*/
  596.     Milestone = 25;
  597.     /*=============================================*/
  598.     /*... BMinusU2 = nextafter(Radix, 0) */
  599.     BMinusU2 = Radix - One;
  600.     BMinusU2 = (BMinusU2 - U2) + One;
  601.     /* Purify Integers */
  602.     if (Radix != One)  {
  603.         X = - TwoForty * LOG(U1) / LOG(Radix);
  604.         Y = FLOOR(Half + X);
  605.         if (FABS(X - Y) * Four < One) X = Y;
  606.         Precision = X / TwoForty;
  607.         Y = FLOOR(Half + Precision);
  608.         if (FABS(Precision - Y) * TwoForty < Half) Precision = Y;
  609.         }
  610.     if ((Precision != FLOOR(Precision)) || (Radix == One)) {
  611.         printf("Precision cannot be characterized by an Integer number\n");
  612.         printf("of significant digits but, by itself, this is a minor flaw.\n");
  613.         }
  614.     if (Radix == One) 
  615.         printf("logarithmic encoding has precision characterized solely by U1.\n");
  616.     else printf("The number of significant digits of the Radix is %f .\n",
  617.             Precision);
  618.     TstCond (Serious, U2 * Nine * Nine * TwoForty < One,
  619.            "Precision worse than 5 decimal figures  ");
  620.     /*=============================================*/
  621.     Milestone = 30;
  622.     /*=============================================*/
  623.     /* Test for extra-precise subepressions */
  624.     X = FABS(((Four / Three - One) - One / Four) * Three - One / Four);
  625.     do  {
  626.         Z2 = X;
  627.         X = (One + (Half * Z2 + ThirtyTwo * Z2 * Z2)) - One;
  628.         } while ( ! ((Z2 <= X) || (X <= Zero)));
  629.     X = Y = Z = FABS((Three / Four - Two / Three) * Three - One / Four);
  630.     do  {
  631.         Z1 = Z;
  632.         Z = (One / Two - ((One / Two - (Half * Z1 + ThirtyTwo * Z1 * Z1))
  633.             + One / Two)) + One / Two;
  634.         } while ( ! ((Z1 <= Z) || (Z <= Zero)));
  635.     do  {
  636.         do  {
  637.             Y1 = Y;
  638.             Y = (Half - ((Half - (Half * Y1 + ThirtyTwo * Y1 * Y1)) + Half
  639.                 )) + Half;
  640.             } while ( ! ((Y1 <= Y) || (Y <= Zero)));
  641.         X1 = X;
  642.         X = ((Half * X1 + ThirtyTwo * X1 * X1) - F9) + F9;
  643.         } while ( ! ((X1 <= X) || (X <= Zero)));
  644.     if ((X1 != Y1) || (X1 != Z1)) {
  645.         BadCond(Serious, "Disagreements among the values X1, Y1, Z1,\n");
  646.         printf("respectively  %.7e,  %.7e,  %.7e,\n", X1, Y1, Z1);
  647.         printf("are symptoms of inconsistencies introduced\n");
  648.         printf("by extra-precise evaluation of arithmetic subexpressions.\n");
  649.         notify("Possibly some part of this");
  650.         if ((X1 == U1) || (Y1 == U1) || (Z1 == U1))  printf(
  651.             "That feature is not tested further by this program.\n") ;
  652.         }
  653.     else  {
  654.         if ((Z1 != U1) || (Z2 != U2)) {
  655.             if ((Z1 >= U1) || (Z2 >= U2)) {
  656.                 BadCond(Failure, "");
  657.                 notify("Precision");
  658.                 printf("\tU1 = %.7e, Z1 - U1 = %.7e\n",U1,Z1-U1);
  659.                 printf("\tU2 = %.7e, Z2 - U2 = %.7e\n",U2,Z2-U2);
  660.                 }
  661.             else {
  662.                 if ((Z1 <= Zero) || (Z2 <= Zero)) {
  663.                     printf("Because of unusual Radix = %f", Radix);
  664.                     printf(", or exact rational arithmetic a result\n");
  665.                     printf("Z1 = %.7e, or Z2 = %.7e ", Z1, Z2);
  666.                     notify("of an\nextra-precision");
  667.                     }
  668.                 if (Z1 != Z2 || Z1 > Zero) {
  669.                     X = Z1 / U1;
  670.                     Y = Z2 / U2;
  671.                     if (Y > X) X = Y;
  672.                     Q = - LOG(X);
  673.                     printf("Some subexpressions appear to be calculated extra\n");
  674.                     printf("precisely with about %g extra B-digits, i.e.\n",
  675.                         (Q / LOG(Radix)));
  676.                     printf("roughly %g extra significant decimals.\n",
  677.                         Q / LOG(10.));
  678.                     }
  679.                 printf("That feature is not tested further by this program.\n");
  680.                 }
  681.             }
  682.         }
  683.     Pause();
  684.     /*=============================================*/
  685.     /*SPLIT
  686.     }
  687. #include "paranoia.h"
  688. void part3(VOID){
  689. */
  690.     Milestone = 35;
  691.     /*=============================================*/
  692.     if (Radix >= Two) {
  693.         X = W / (Radix * Radix);
  694.         Y = X + One;
  695.         Z = Y - X;
  696.         T = Z + U2;
  697.         X = T - Z;
  698.         TstCond (Failure, X == U2,
  699.             "Subtraction is not normalized X=Y,X+Z != Y+Z!");
  700.         if (X == U2) printf(
  701.             "Subtraction appears to be normalized, as it should be.");
  702.         }
  703.     printf("\nChecking for guard digit in *, /, and -.\n");
  704.     Y = F9 * One;
  705.     Z = One * F9;
  706.     X = F9 - Half;
  707.     Y = (Y - Half) - X;
  708.     Z = (Z - Half) - X;
  709.     X = One + U2;
  710.     T = X * Radix;
  711.     R = Radix * X;
  712.     X = T - Radix;
  713.     X = X - Radix * U2;
  714.     T = R - Radix;
  715.     T = T - Radix * U2;
  716.     X = X * (Radix - One);
  717.     T = T * (Radix - One);
  718.     if ((X == Zero) && (Y == Zero) && (Z == Zero) && (T == Zero)) GMult = Yes;
  719.     else {
  720.         GMult = No;
  721.         TstCond (Serious, False,
  722.             "* lacks a Guard Digit, so 1*X != X");
  723.         }
  724.     Z = Radix * U2;
  725.     X = One + Z;
  726.     Y = FABS((X + Z) - X * X) - U2;
  727.     X = One - U2;
  728.     Z = FABS((X - U2) - X * X) - U1;
  729.     TstCond (Failure, (Y <= Zero)
  730.            && (Z <= Zero), "* gets too many final digits wrong.\n");
  731.     Y = One - U2;
  732.     X = One + U2;
  733.     Z = One / Y;
  734.     Y = Z - X;
  735.     X = One / Three;
  736.     Z = Three / Nine;
  737.     X = X - Z;
  738.     T = Nine / TwentySeven;
  739.     Z = Z - T;
  740.     TstCond(Defect, X == Zero && Y == Zero && Z == Zero,
  741.         "Division lacks a Guard Digit, so error can exceed 1 ulp\n\
  742. or  1/3  and  3/9  and  9/27 may disagree");
  743.     Y = F9 / One;
  744.     X = F9 - Half;
  745.     Y = (Y - Half) - X;
  746.     X = One + U2;
  747.     T = X / One;
  748.     X = T - X;
  749.     if ((X == Zero) && (Y == Zero) && (Z == Zero)) GDiv = Yes;
  750.     else {
  751.         GDiv = No;
  752.         TstCond (Serious, False,
  753.             "Division lacks a Guard Digit, so X/1 != X");
  754.         }
  755.     X = One / (One + U2);
  756.     Y = X - Half - Half;
  757.     TstCond (Serious, Y < Zero,
  758.            "Computed value of 1/1.000..1 >= 1");
  759.     X = One - U2;
  760.     Y = One + Radix * U2;
  761.     Z = X * Radix;
  762.     T = Y * Radix;
  763.     R = Z / Radix;
  764.     StickyBit = T / Radix;
  765.     X = R - X;
  766.     Y = StickyBit - Y;
  767.     TstCond (Failure, X == Zero && Y == Zero,
  768.             "* and/or / gets too many last digits wrong");
  769.     Y = One - U1;
  770.     X = One - F9;
  771.     Y = One - Y;
  772.     T = Radix - U2;
  773.     Z = Radix - BMinusU2;
  774.     T = Radix - T;
  775.     if ((X == U1) && (Y == U1) && (Z == U2) && (T == U2)) GAddSub = Yes;
  776.     else {
  777.         GAddSub = No;
  778.         TstCond (Serious, False,
  779.             "- lacks Guard Digit, so cancellation is obscured");
  780.         }
  781.     if (F9 != One && F9 - One >= Zero) {
  782.         BadCond(Serious, "comparison alleges  (1-U1) < 1  although\n");
  783.         printf("  subtraction yields  (1-U1) - 1 = 0 , thereby vitiating\n");
  784.         printf("  such precautions against division by zero as\n");
  785.         printf("  ...  if (X == 1.0) {.....} else {.../(X-1.0)...}\n");
  786.         }
  787.     if (GMult == Yes && GDiv == Yes && GAddSub == Yes) printf(
  788.         "     *, /, and - appear to have guard digits, as they should.\n");
  789.     /*=============================================*/
  790.     Milestone = 40;
  791.     /*=============================================*/
  792.     Pause();
  793.     printf("Checking rounding on multiply, divide and add/subtract.\n");
  794.     RMult = Other;
  795.     RDiv = Other;
  796.     RAddSub = Other;
  797.     RadixD2 = Radix / Two;
  798.     A1 = Two;
  799.     Done = False;
  800.     do  {
  801.         AInvrse = Radix;
  802.         do  {
  803.             X = AInvrse;
  804.             AInvrse = AInvrse / A1;
  805.             } while ( ! (FLOOR(AInvrse) != AInvrse));
  806.         Done = (X == One) || (A1 > Three);
  807.         if (! Done) A1 = Nine + One;
  808.         } while ( ! (Done));
  809.     if (X == One) A1 = Radix;
  810.     AInvrse = One / A1;
  811.     X = A1;
  812.     Y = AInvrse;
  813.     Done = False;
  814.     do  {
  815.         Z = X * Y - Half;
  816.         TstCond (Failure, Z == Half,
  817.             "X * (1/X) differs from 1");
  818.         Done = X == Radix;
  819.         X = Radix;
  820.         Y = One / X;
  821.         } while ( ! (Done));
  822.     Y2 = One + U2;
  823.     Y1 = One - U2;
  824.     X = OneAndHalf - U2;
  825.     Y = OneAndHalf + U2;
  826.     Z = (X - U2) * Y2;
  827.     T = Y * Y1;
  828.     Z = Z - X;
  829.     T = T - X;
  830.     X = X * Y2;
  831.     Y = (Y + U2) * Y1;
  832.     X = X - OneAndHalf;
  833.     Y = Y - OneAndHalf;
  834.     if ((X == Zero) && (Y == Zero) && (Z == Zero) && (T <= Zero)) {
  835.         X = (OneAndHalf + U2) * Y2;
  836.         Y = OneAndHalf - U2 - U2;
  837.         Z = OneAndHalf + U2 + U2;
  838.         T = (OneAndHalf - U2) * Y1;
  839.         X = X - (Z + U2);
  840.         StickyBit = Y * Y1;
  841.         S = Z * Y2;
  842.         T = T - Y;
  843.         Y = (U2 - Y) + StickyBit;
  844.         Z = S - (Z + U2 + U2);
  845.         StickyBit = (Y2 + U2) * Y1;
  846.         Y1 = Y2 * Y1;
  847.         StickyBit = StickyBit - Y2;
  848.         Y1 = Y1 - Half;
  849.         if ((X == Zero) && (Y == Zero) && (Z == Zero) && (T == Zero)
  850.             && ( StickyBit == Zero) && (Y1 == Half)) {
  851.             RMult = Rounded;
  852.             printf("Multiplication appears to round correctly.\n");
  853.             }
  854.         else    if ((X + U2 == Zero) && (Y < Zero) && (Z + U2 == Zero)
  855.                 && (T < Zero) && (StickyBit + U2 == Zero)
  856.                 && (Y1 < Half)) {
  857.                 RMult = Chopped;
  858.                 printf("Multiplication appears to chop.\n");
  859.                 }
  860.             else printf("* is neither chopped nor correctly rounded.\n");
  861.         if ((RMult == Rounded) && (GMult == No)) notify("Multiplication");
  862.         }
  863.     else printf("* is neither chopped nor correctly rounded.\n");
  864.     /*=============================================*/
  865.     Milestone = 45;
  866.     /*=============================================*/
  867.     Y2 = One + U2;
  868.     Y1 = One - U2;
  869.     Z = OneAndHalf + U2 + U2;
  870.     X = Z / Y2;
  871.     T = OneAndHalf - U2 - U2;
  872.     Y = (T - U2) / Y1;
  873.     Z = (Z + U2) / Y2;
  874.     X = X - OneAndHalf;
  875.     Y = Y - T;
  876.     T = T / Y1;
  877.     Z = Z - (OneAndHalf + U2);
  878.     T = (U2 - OneAndHalf) + T;
  879.     if (! ((X > Zero) || (Y > Zero) || (Z > Zero) || (T > Zero))) {
  880.         X = OneAndHalf / Y2;
  881.         Y = OneAndHalf - U2;
  882.         Z = OneAndHalf + U2;
  883.         X = X - Y;
  884.         T = OneAndHalf / Y1;
  885.         Y = Y / Y1;
  886.         T = T - (Z + U2);
  887.         Y = Y - Z;
  888.         Z = Z / Y2;
  889.         Y1 = (Y2 + U2) / Y2;
  890.         Z = Z - OneAndHalf;
  891.         Y2 = Y1 - Y2;
  892.         Y1 = (F9 - U1) / F9;
  893.         if ((X == Zero) && (Y == Zero) && (Z == Zero) && (T == Zero)
  894.             && (Y2 == Zero) && (Y2 == Zero)
  895.             && (Y1 - Half == F9 - Half )) {
  896.             RDiv = Rounded;
  897.             printf("Division appears to round correctly.\n");
  898.             if (GDiv == No) notify("Division");
  899.             }
  900.         else if ((X < Zero) && (Y < Zero) && (Z < Zero) && (T < Zero)
  901.             && (Y2 < Zero) && (Y1 - Half < F9 - Half)) {
  902.             RDiv = Chopped;
  903.             printf("Division appears to chop.\n");
  904.             }
  905.         }
  906.     if (RDiv == Other) printf("/ is neither chopped nor correctly rounded.\n");
  907.     BInvrse = One / Radix;
  908.     TstCond (Failure, (BInvrse * Radix - Half == Half),
  909.            "Radix * ( 1 / Radix ) differs from 1");
  910.     /*=============================================*/
  911.     /*SPLIT
  912.     }
  913. #include "paranoia.h"
  914. void part4(VOID){
  915. */
  916.     Milestone = 50;
  917.     /*=============================================*/
  918.     TstCond (Failure, ((F9 + U1) - Half == Half)
  919.            && ((BMinusU2 + U2 ) - One == Radix - One),
  920.            "Incomplete carry-propagation in Addition");
  921.     X = One - U1 * U1;
  922.     Y = One + U2 * (One - U2);
  923.     Z = F9 - Half;
  924.     X = (X - Half) - Z;
  925.     Y = Y - One;
  926.     if ((X == Zero) && (Y == Zero)) {
  927.         RAddSub = Chopped;
  928.         printf("Add/Subtract appears to be chopped.\n");
  929.         }
  930.     if (GAddSub == Yes) {
  931.         X = (Half + U2) * U2;
  932.         Y = (Half - U2) * U2;
  933.         X = One + X;
  934.         Y = One + Y;
  935.         X = (One + U2) - X;
  936.         Y = One - Y;
  937.         if ((X == Zero) && (Y == Zero)) {
  938.             X = (Half + U2) * U1;
  939.             Y = (Half - U2) * U1;
  940.             X = One - X;
  941.             Y = One - Y;
  942.             X = F9 - X;
  943.             Y = One - Y;
  944.             if ((X == Zero) && (Y == Zero)) {
  945.                 RAddSub = Rounded;
  946.                 printf("Addition/Subtraction appears to round correctly.\n");
  947.                 if (GAddSub == No) notify("Add/Subtract");
  948.                 }
  949.             else printf("Addition/Subtraction neither rounds nor chops.\n");
  950.             }
  951.         else printf("Addition/Subtraction neither rounds nor chops.\n");
  952.         }
  953.     else printf("Addition/Subtraction neither rounds nor chops.\n");
  954.     S = One;
  955.     X = One + Half * (One + Half);
  956.     Y = (One + U2) * Half;
  957.     Z = X - Y;
  958.     T = Y - X;
  959.     StickyBit = Z + T;
  960.     if (StickyBit != Zero) {
  961.         S = Zero;
  962.         BadCond(Flaw, "(X - Y) + (Y - X) is non zero!\n");
  963.         }
  964.     StickyBit = Zero;
  965.     if ((GMult == Yes) && (GDiv == Yes) && (GAddSub == Yes)
  966.         && (RMult == Rounded) && (RDiv == Rounded)
  967.         && (RAddSub == Rounded) && (FLOOR(RadixD2) == RadixD2)) {
  968.         printf("Checking for sticky bit.\n");
  969.         X = (Half + U1) * U2;
  970.         Y = Half * U2;
  971.         Z = One + Y;
  972.         T = One + X;
  973.         if ((Z - One <= Zero) && (T - One >= U2)) {
  974.             Z = T + Y;
  975.             Y = Z - X;
  976.             if ((Z - T >= U2) && (Y - T == Zero)) {
  977.                 X = (Half + U1) * U1;
  978.                 Y = Half * U1;
  979.                 Z = One - Y;
  980.                 T = One - X;
  981.                 if ((Z - One == Zero) && (T - F9 == Zero)) {
  982.                     Z = (Half - U1) * U1;
  983.                     T = F9 - Z;
  984.                     Q = F9 - Y;
  985.                     if ((T - F9 == Zero) && (F9 - U1 - Q == Zero)) {
  986.                         Z = (One + U2) * OneAndHalf;
  987.                         T = (OneAndHalf + U2) - Z + U2;
  988.                         X = One + Half / Radix;
  989.                         Y = One + Radix * U2;
  990.                         Z = X * Y;
  991.                         if (T == Zero && X + Radix * U2 - Z == Zero) {
  992.                             if (Radix != Two) {
  993.                                 X = Two + U2;
  994.                                 Y = X / Two;
  995.                                 if ((Y - One == Zero)) StickyBit = S;
  996.                                 }
  997.                             else StickyBit = S;
  998.                             }
  999.                         }
  1000.                     }
  1001.                 }
  1002.             }
  1003.         }
  1004.     if (StickyBit == One) printf("Sticky bit apparently used correctly.\n");
  1005.     else printf("Sticky bit used incorrectly or not at all.\n");
  1006.     TstCond (Flaw, !(GMult == No || GDiv == No || GAddSub == No ||
  1007.             RMult == Other || RDiv == Other || RAddSub == Other),
  1008.         "lack(s) of guard digits or failure(s) to correctly round or chop\n\
  1009. (noted above) count as one flaw in the final tally below");
  1010.     /*=============================================*/
  1011.     Milestone = 60;
  1012.     /*=============================================*/
  1013.     printf("\n");
  1014.     printf("Does Multiplication commute?  ");
  1015.     printf("Testing on %d random pairs.\n", NoTrials);
  1016.     Random9 = SQRT(3.0);
  1017.     Random1 = Third;
  1018.     I = 1;
  1019.     do  {
  1020.         X = Random();
  1021.         Y = Random();
  1022.         Z9 = Y * X;
  1023.         Z = X * Y;
  1024.         Z9 = Z - Z9;
  1025.         I = I + 1;
  1026.         } while ( ! ((I > NoTrials) || (Z9 != Zero)));
  1027.     if (I == NoTrials) {
  1028.         Random1 = One + Half / Three;
  1029.         Random2 = (U2 + U1) + One;
  1030.         Z = Random1 * Random2;
  1031.         Y = Random2 * Random1;
  1032.         Z9 = (One + Half / Three) * ((U2 + U1) + One) - (One + Half /
  1033.             Three) * ((U2 + U1) + One);
  1034.         }
  1035.     if (! ((I == NoTrials) || (Z9 == Zero)))
  1036.         BadCond(Defect, "X * Y == Y * X trial fails.\n");
  1037.     else printf("     No failures found in %d integer pairs.\n", NoTrials);
  1038.     /*=============================================*/
  1039.     Milestone = 70;
  1040.     /*=============================================*/
  1041.     printf("\nRunning test of square root(x).\n");
  1042.     TstCond (Failure, (Zero == SQRT(Zero))
  1043.            && (- Zero == SQRT(- Zero))
  1044.            && (One == SQRT(One)), "Square root of 0.0, -0.0 or 1.0 wrong");
  1045.     MinSqEr = Zero;
  1046.     MaxSqEr = Zero;
  1047.     J = Zero;
  1048.     X = Radix;
  1049.     OneUlp = U2;
  1050.     SqXMinX (Serious);
  1051.     X = BInvrse;
  1052.     OneUlp = BInvrse * U1;
  1053.     SqXMinX (Serious);
  1054.     X = U1;
  1055.     OneUlp = U1 * U1;
  1056.     SqXMinX (Serious);
  1057.     if (J != Zero) Pause();
  1058.     printf("Testing if sqrt(X * X) == X for %d Integers X.\n", NoTrials);
  1059.     J = Zero;
  1060.     X = Two;
  1061.     Y = Radix;
  1062.     if ((Radix != One)) do  {
  1063.         X = Y;
  1064.         Y = Radix * Y;
  1065.         } while ( ! ((Y - X >= NoTrials)));
  1066.     OneUlp = X * U2;
  1067.     I = 1;
  1068.     while (I <= NoTrials) {
  1069.         X = X + One;
  1070.         SqXMinX (Defect);
  1071.         if (J > Zero) break;
  1072.         I = I + 1;
  1073.         }
  1074.     printf("Test for sqrt monotonicity.\n");
  1075.     I = - 1;
  1076.     X = BMinusU2;
  1077.     Y = Radix;
  1078.     Z = Radix + Radix * U2;
  1079.     NotMonot = False;
  1080.     Monot = False;
  1081.     while ( ! (NotMonot || Monot)) {
  1082.         I = I + 1;
  1083.         X = SQRT(X);
  1084.         Q = SQRT(Y);
  1085.         Z = SQRT(Z);
  1086.         if ((X > Q) || (Q > Z)) NotMonot = True;
  1087.         else {
  1088.             Q = FLOOR(Q + Half);
  1089.             if ((I > 0) || (Radix == Q * Q)) Monot = True;
  1090.             else if (I > 0) {
  1091.             if (I > 1) Monot = True;
  1092.             else {
  1093.                 Y = Y * BInvrse;
  1094.                 X = Y - U1;
  1095.                 Z = Y + U1;
  1096.                 }
  1097.             }
  1098.             else {
  1099.                 Y = Q;
  1100.                 X = Y - U2;
  1101.                 Z = Y + U2;
  1102.                 }
  1103.             }
  1104.         }
  1105.     if (Monot) printf("sqrt has passed a test for Monotonicity.\n");
  1106.     else {
  1107.         BadCond(Defect, "");
  1108.         printf("sqrt(X) is non-monotonic for X near %.7e .\n", Y);
  1109.         }
  1110.     /*=============================================*/
  1111.     /*SPLIT
  1112.     }
  1113. #include "paranoia.h"
  1114. void part5(VOID){
  1115. */
  1116.     Milestone = 80;
  1117.     /*=============================================*/
  1118.     MinSqEr = MinSqEr + Half;
  1119.     MaxSqEr = MaxSqEr - Half;
  1120.     Y = (SQRT(One + U2) - One) / U2;
  1121.     SqEr = (Y - One) + U2 / Eight;
  1122.     if (SqEr > MaxSqEr) MaxSqEr = SqEr;
  1123.     SqEr = Y + U2 / Eight;
  1124.     if (SqEr < MinSqEr) MinSqEr = SqEr;
  1125.     Y = ((SQRT(F9) - U2) - (One - U2)) / U1;
  1126.     SqEr = Y + U1 / Eight;
  1127.     if (SqEr > MaxSqEr) MaxSqEr = SqEr;
  1128.     SqEr = (Y + One) + U1 / Eight;
  1129.     if (SqEr < MinSqEr) MinSqEr = SqEr;
  1130.     OneUlp = U2;
  1131.     X = OneUlp;
  1132.     for( Indx = 1; Indx <= 3; ++Indx) {
  1133.         Y = SQRT((X + U1 + X) + F9);
  1134.         Y = ((Y - U2) - ((One - U2) + X)) / OneUlp;
  1135.         Z = ((U1 - X) + F9) * Half * X * X / OneUlp;
  1136.         SqEr = (Y + Half) + Z;
  1137.         if (SqEr < MinSqEr) MinSqEr = SqEr;
  1138.         SqEr = (Y - Half) + Z;
  1139.         if (SqEr > MaxSqEr) MaxSqEr = SqEr;
  1140.         if (((Indx == 1) || (Indx == 3))) 
  1141.             X = OneUlp * Sign (X) * FLOOR(Eight / (Nine * SQRT(OneUlp)));
  1142.         else {
  1143.             OneUlp = U1;
  1144.             X = - OneUlp;
  1145.             }
  1146.         }
  1147.     /*=============================================*/
  1148.     Milestone = 85;
  1149.     /*=============================================*/
  1150.     SqRWrng = False;
  1151.     Anomaly = False;
  1152.     RSqrt = Other; /* ~dgh */
  1153.     if (Radix != One) {
  1154.         printf("Testing whether sqrt is rounded or chopped.\n");
  1155.         D = FLOOR(Half + POW(Radix, One + Precision - FLOOR(Precision)));
  1156.     /* ... == Radix^(1 + fract) if (Precision == Integer + fract. */
  1157.         X = D / Radix;
  1158.         Y = D / A1;
  1159.         if ((X != FLOOR(X)) || (Y != FLOOR(Y))) {
  1160.             Anomaly = True;
  1161.             }
  1162.         else {
  1163.             X = Zero;
  1164.             Z2 = X;
  1165.             Y = One;
  1166.             Y2 = Y;
  1167.             Z1 = Radix - One;
  1168.             FourD = Four * D;
  1169.             do  {
  1170.                 if (Y2 > Z2) {
  1171.                     Q = Radix;
  1172.                     Y1 = Y;
  1173.                     do  {
  1174.                         X1 = FABS(Q + FLOOR(Half - Q / Y1) * Y1);
  1175.                         Q = Y1;
  1176.                         Y1 = X1;
  1177.                         } while ( ! (X1 <= Zero));
  1178.                     if (Q <= One) {
  1179.                         Z2 = Y2;
  1180.                         Z = Y;
  1181.                         }
  1182.                     }
  1183.                 Y = Y + Two;
  1184.                 X = X + Eight;
  1185.                 Y2 = Y2 + X;
  1186.                 if (Y2 >= FourD) Y2 = Y2 - FourD;
  1187.                 } while ( ! (Y >= D));
  1188.             X8 = FourD - Z2;
  1189.             Q = (X8 + Z * Z) / FourD;
  1190.             X8 = X8 / Eight;
  1191.             if (Q != FLOOR(Q)) Anomaly = True;
  1192.             else {
  1193.                 Break = False;
  1194.                 do  {
  1195.                     X = Z1 * Z;
  1196.                     X = X - FLOOR(X / Radix) * Radix;
  1197.                     if (X == One) 
  1198.                         Break = True;
  1199.                     else
  1200.                         Z1 = Z1 - One;
  1201.                     } while ( ! (Break || (Z1 <= Zero)));
  1202.                 if ((Z1 <= Zero) && (! Break)) Anomaly = True;
  1203.                 else {
  1204.                     if (Z1 > RadixD2) Z1 = Z1 - Radix;
  1205.                     do  {
  1206.                         NewD();
  1207.                         } while ( ! (U2 * D >= F9));
  1208.                     if (D * Radix - D != W - D) Anomaly = True;
  1209.                     else {
  1210.                         Z2 = D;
  1211.                         I = 0;
  1212.                         Y = D + (One + Z) * Half;
  1213.                         X = D + Z + Q;
  1214.                         SR3750();
  1215.                         Y = D + (One - Z) * Half + D;
  1216.                         X = D - Z + D;
  1217.                         X = X + Q + X;
  1218.                         SR3750();
  1219.                         NewD();
  1220.                         if (D - Z2 != W - Z2) Anomaly = True;
  1221.                         else {
  1222.                             Y = (D - Z2) + (Z2 + (One - Z) * Half);
  1223.                             X = (D - Z2) + (Z2 - Z + Q);
  1224.                             SR3750();
  1225.                             Y = (One + Z) * Half;
  1226.                             X = Q;
  1227.                             SR3750();
  1228.                             if (I == 0) Anomaly = True;
  1229.                             }
  1230.                         }
  1231.                     }
  1232.                 }
  1233.             }
  1234.         if ((I == 0) || Anomaly) {
  1235.             BadCond(Failure, "Anomalous arithmetic with Integer < ");
  1236.             printf("Radix^Precision = %.7e\n", W);
  1237.             printf(" fails test whether sqrt rounds or chops.\n");
  1238.             SqRWrng = True;
  1239.             }
  1240.         }
  1241.     if (! Anomaly) {
  1242.         if (! ((MinSqEr < Zero) || (MaxSqEr > Zero))) {
  1243.             RSqrt = Rounded;
  1244.             printf("Square root appears to be correctly rounded.\n");
  1245.             }
  1246.         else  {
  1247.             if ((MaxSqEr + U2 > U2 - Half) || (MinSqEr > Half)
  1248.                 || (MinSqEr + Radix < Half)) SqRWrng = True;
  1249.             else {
  1250.                 RSqrt = Chopped;
  1251.                 printf("Square root appears to be chopped.\n");
  1252.                 }
  1253.             }
  1254.         }
  1255.     if (SqRWrng) {
  1256.         printf("Square root is neither chopped nor correctly rounded.\n");
  1257.         printf("Observed errors run from %.7e ", MinSqEr - Half);
  1258.         printf("to %.7e ulps.\n", Half + MaxSqEr);
  1259.         TstCond (Serious, MaxSqEr - MinSqEr < Radix * Radix,
  1260.             "sqrt gets too many last digits wrong");
  1261.         }
  1262.     /*=============================================*/
  1263.     Milestone = 90;
  1264.     /*=============================================*/
  1265.     Pause();
  1266.     printf("Testing powers Z^i for small Integers Z and i.\n");
  1267.     N = 0;
  1268.     /* ... test powers of zero. */
  1269.     I = 0;
  1270.     Z = -Zero;
  1271.     M = 3;
  1272.     Break = False;
  1273.     do  {
  1274.         X = One;
  1275.         sigsave = sigfpe;
  1276.         if (setjmp(ovfl_buf)) {
  1277.             printf("SR3980 failed for Z = %f, Q = %f!\n", Z, Q);
  1278.             }
  1279.         else  SR3980();
  1280.         sigsave = 0;
  1281.         if (I <= 10) {
  1282.             I = 1023;
  1283.                 sigsave = sigfpe;
  1284.                 if (setjmp(ovfl_buf))
  1285.                    printf("SR3980 failed for Z = %f, Q = %f!\n",Z,Q);
  1286.             else   SR3980();
  1287.                         sigsave = 0;
  1288.             }
  1289.         if (Z == MinusOne) Break = True;
  1290.         else {
  1291.             Z = MinusOne;
  1292.             /* .. if(-1)^N is invalid, replace MinusOne by One. */
  1293.             I = - 4;
  1294.             }
  1295.         } while ( ! Break);
  1296.     PrintIfNPositive();
  1297.     N1 = N;
  1298.     N = 0;
  1299.     Z = A1;
  1300.     M = (int)FLOOR(Two * LOG(W) / LOG(A1));
  1301.     Break = False;
  1302.     do  {
  1303.         X = Z;
  1304.         I = 1;
  1305.         SR3980();
  1306.         if (Z == AInvrse) Break = True;
  1307.         else Z = AInvrse;
  1308.         } while ( ! (Break));
  1309.     /*=============================================*/
  1310.         Milestone = 100;
  1311.     /*=============================================*/
  1312.     /*  Powers of Radix have been tested, */
  1313.     /*         next try a few primes     */
  1314.     M = NoTrials;
  1315.     Z = Three;
  1316.     do  {
  1317.         X = Z;
  1318.         I = 1;
  1319.         SR3980();
  1320.         do  {
  1321.             Z = Z + Two;
  1322.             } while ( Three * FLOOR(Z / Three) == Z );
  1323.         } while ( Z < Eight * Three );
  1324.     if (N > 0) {
  1325.         printf("Errors like this may invalidate financial calculations\n");
  1326.         printf("\tinvolving interest rates.\n");
  1327.         }
  1328.     PrintIfNPositive();
  1329.     N += N1;
  1330.     if (N == 0) printf("... no discrepancies found.\n");
  1331.     if (N > 0) Pause();
  1332.     else printf("\n");
  1333.     /*=============================================*/
  1334.     /*SPLIT
  1335.     }
  1336. #include "paranoia.h"
  1337. void part6(VOID){
  1338. */
  1339.     Milestone = 110;
  1340.     /*=============================================*/
  1341.     printf("Seeking Underflow thresholds UfThold and E0.\n");
  1342.     D = U1;
  1343.     if (Precision != FLOOR(Precision)) {
  1344.         D = BInvrse;
  1345.         X = Precision;
  1346.         do  {
  1347.             D = D * BInvrse;
  1348.             X = X - One;
  1349.             } while ( X > Zero);
  1350.         }
  1351.     Y = One;
  1352.     Z = D;
  1353.     /* ... D is power of 1/Radix < 1. */
  1354.     do  {
  1355.         C = Y;
  1356.         Y = Z;
  1357.         Z = Y * Y;
  1358.         } while ((Y > Z) && (Z + Z > Z));
  1359.     Y = C;
  1360.     Z = Y * D;
  1361.     do  {
  1362.         C = Y;
  1363.         Y = Z;
  1364.         Z = Y * D;
  1365.         } while ((Y > Z) && (Z + Z > Z));
  1366.     if (Radix < Two) HInvrse = Two;
  1367.     else HInvrse = Radix;
  1368.     H = One / HInvrse;
  1369.     /* ... 1/HInvrse == H == Min(1/Radix, 1/2) */
  1370.     CInvrse = One / C;
  1371.     E0 = C;
  1372.     Z = E0 * H;
  1373.     /* ...1/Radix^(BIG Integer) << 1 << CInvrse == 1/C */
  1374.     do  {
  1375.         Y = E0;
  1376.         E0 = Z;
  1377.         Z = E0 * H;
  1378.         } while ((E0 > Z) && (Z + Z > Z));
  1379.     UfThold = E0;
  1380.     E1 = Zero;
  1381.     Q = Zero;
  1382.     E9 = U2;
  1383.     S = One + E9;
  1384.     D = C * S;
  1385.     if (D <= C) {
  1386.         E9 = Radix * U2;
  1387.         S = One + E9;
  1388.         D = C * S;
  1389.         if (D <= C) {
  1390.             BadCond(Failure, "multiplication gets too many last digits wrong.\n");
  1391.             Underflow = E0;
  1392.             Y1 = Zero;
  1393.             PseudoZero = Z;
  1394.             Pause();
  1395.             }
  1396.         }
  1397.     else {
  1398.         Underflow = D;
  1399.         PseudoZero = Underflow * H;
  1400.         UfThold = Zero;
  1401.         do  {
  1402.             Y1 = Underflow;
  1403.             Underflow = PseudoZero;
  1404.             if (E1 + E1 <= E1) {
  1405.                 Y2 = Underflow * HInvrse;
  1406.                 E1 = FABS(Y1 - Y2);
  1407.                 Q = Y1;
  1408.                 if ((UfThold == Zero) && (Y1 != Y2)) UfThold = Y1;
  1409.                 }
  1410.             PseudoZero = PseudoZero * H;
  1411.             } while ((Underflow > PseudoZero)
  1412.                 && (PseudoZero + PseudoZero > PseudoZero));
  1413.         }
  1414.     /* Comment line 4530 .. 4560 */
  1415.     if (PseudoZero != Zero) {
  1416.         printf("\n");
  1417.         Z = PseudoZero;
  1418.     /* ... Test PseudoZero for "phoney- zero" violates */
  1419.     /* ... PseudoZero < Underflow or PseudoZero < PseudoZero + PseudoZero
  1420.            ... */
  1421.         if (PseudoZero <= Zero) {
  1422.             BadCond(Failure, "Positive expressions can underflow to an\n");
  1423.             printf("allegedly negative value\n");
  1424.             printf("PseudoZero that prints out as: %g .\n", PseudoZero);
  1425.             X = - PseudoZero;
  1426.             if (X <= Zero) {
  1427.                 printf("But -PseudoZero, which should be\n");
  1428.                 printf("positive, isn't; it prints out as  %g .\n", X);
  1429.                 }
  1430.             }
  1431.         else {
  1432.             BadCond(Flaw, "Underflow can stick at an allegedly positive\n");
  1433.             printf("value PseudoZero that prints out as %g .\n", PseudoZero);
  1434.             }
  1435.         TstPtUf();
  1436.         }
  1437.     /*=============================================*/
  1438.     Milestone = 120;
  1439.     /*=============================================*/
  1440.     if (CInvrse * Y > CInvrse * Y1) {
  1441.         S = H * S;
  1442.         E0 = Underflow;
  1443.         }
  1444.     if (! ((E1 == Zero) || (E1 == E0))) {
  1445.         BadCond(Defect, "");
  1446.         if (E1 < E0) {
  1447.             printf("Products underflow at a higher");
  1448.             printf(" threshold than differences.\n");
  1449.             if (PseudoZero == Zero) 
  1450.             E0 = E1;
  1451.             }
  1452.         else {
  1453.             printf("Difference underflows at a higher");
  1454.             printf(" threshold than products.\n");
  1455.             }
  1456.         }
  1457.     printf("Smallest strictly positive number found is E0 = %g .\n", E0);
  1458.     Z = E0;
  1459.     TstPtUf();
  1460.     Underflow = E0;
  1461.     if (N == 1) Underflow = Y;
  1462.     I = 4;
  1463.     if (E1 == Zero) I = 3;
  1464.     if (UfThold == Zero) I = I - 2;
  1465.     UfNGrad = True;
  1466.     switch (I)  {
  1467.         case    1:
  1468.         UfThold = Underflow;
  1469.         if ((CInvrse * Q) != ((CInvrse * Y) * S)) {
  1470.             UfThold = Y;
  1471.             BadCond(Failure, "Either accuracy deteriorates as numbers\n");
  1472.             printf("approach a threshold = %.17e\n", UfThold);;
  1473.             printf(" coming down from %.17e\n", C);
  1474.             printf(" or else multiplication gets too many last digits wrong.\n");
  1475.             }
  1476.         Pause();
  1477.         break;
  1478.     
  1479.         case    2:
  1480.         BadCond(Failure, "Underflow confuses Comparison, which alleges that\n");
  1481.         printf("Q == Y while denying that |Q - Y| == 0; these values\n");
  1482.         printf("print out as Q = %.17e, Y = %.17e .\n", Q, Y2);
  1483.         printf ("|Q - Y| = %.17e .\n" , FABS(Q - Y2));
  1484.         UfThold = Q;
  1485.         break;
  1486.     
  1487.         case    3:
  1488.         X = X;
  1489.         break;
  1490.     
  1491.         case    4:
  1492.         if ((Q == UfThold) && (E1 == E0)
  1493.             && (FABS( UfThold - E1 / E9) <= E1)) {
  1494.             UfNGrad = False;
  1495.             printf("Underflow is gradual; it incurs Absolute Error =\n");
  1496.             printf("(roundoff in UfThold) < E0.\n");
  1497.             Y = E0 * CInvrse;
  1498.             Y = Y * (OneAndHalf + U2);
  1499.             X = CInvrse * (One + U2);
  1500.             Y = Y / X;
  1501.             IEEE = (Y == E0);
  1502.             }
  1503.         }
  1504.     if (UfNGrad) {
  1505.         printf("\n");
  1506.         sigsave = sigfpe;
  1507.         if (setjmp(ovfl_buf)) {
  1508.             printf("Underflow / UfThold failed!\n");
  1509.             R = H + H;
  1510.             }
  1511.         else R = SQRT(Underflow / UfThold);
  1512.         sigsave = 0;
  1513.         if (R <= H) {
  1514.             Z = R * UfThold;
  1515.             X = Z * (One + R * H * (One + H));
  1516.             }
  1517.         else {
  1518.             Z = UfThold;
  1519.             X = Z * (One + H * H * (One + H));
  1520.             }
  1521.         if (! ((X == Z) || (X - Z != Zero))) {
  1522.             BadCond(Flaw, "");
  1523.             printf("X = %.17e\n\tis not equal to Z = %.17e .\n", X, Z);
  1524.             Z9 = X - Z;
  1525.             printf("yet X - Z yields %.17e .\n", Z9);
  1526.             printf("    Should this NOT signal Underflow, ");
  1527.             printf("this is a SERIOUS DEFECT\nthat causes ");
  1528.             printf("confusion when innocent statements like\n");;
  1529.             printf("    if (X == Z)  ...  else");
  1530.             printf("  ... (f(X) - f(Z)) / (X - Z) ...\n");
  1531.             printf("encounter Division by Zero although actually\n");
  1532.             sigsave = sigfpe;
  1533.             if (setjmp(ovfl_buf)) printf("X / Z fails!\n");
  1534.             else printf("X / Z = 1 + %g .\n", (X / Z - Half) - Half);
  1535.             sigsave = 0;
  1536.             }
  1537.         }
  1538.     printf("The Underflow threshold is %.17e, %s\n", UfThold,
  1539.            " below which");
  1540.     printf("calculation may suffer larger Relative error than ");
  1541.     printf("merely roundoff.\n");
  1542.     Y2 = U1 * U1;
  1543.     Y = Y2 * Y2;
  1544.     Y2 = Y * U1;
  1545.     if (Y2 <= UfThold) {
  1546.         if (Y > E0) {
  1547.             BadCond(Defect, "");
  1548.             I = 5;
  1549.             }
  1550.         else {
  1551.             BadCond(Serious, "");
  1552.             I = 4;
  1553.             }
  1554.         printf("Range is too narrow; U1^%d Underflows.\n", I);
  1555.         }
  1556.     /*=============================================*/
  1557.     /*SPLIT
  1558.     }
  1559. #include "paranoia.h"
  1560. void part7(VOID){
  1561. */
  1562.     Milestone = 130;
  1563.     /*=============================================*/
  1564.     Y = - FLOOR(Half - TwoForty * LOG(UfThold) / LOG(HInvrse)) / TwoForty;
  1565.     Y2 = Y + Y;
  1566.     printf("Since underflow occurs below the threshold\n");
  1567.     printf("UfThold = (%.17e) ^ (%.17e)\nonly underflow ", HInvrse, Y);
  1568.     printf("should afflict the expression\n\t(%.17e) ^ (%.17e);\n",
  1569.         HInvrse, Y2);
  1570.     printf("actually calculating yields:");
  1571.     if (setjmp(ovfl_buf)) {
  1572.         sigsave = 0;
  1573.         BadCond(Serious, "trap on underflow.\n");
  1574.         }
  1575.     else {
  1576.         sigsave = sigfpe;
  1577.         V9 = POW(HInvrse, Y2);
  1578.         sigsave = 0;
  1579.         printf(" %.17e .\n", V9);
  1580.         if (! ((V9 >= Zero) && (V9 <= (Radix + Radix + E9) * UfThold))) {
  1581.             BadCond(Serious, "this is not between 0 and underflow\n");
  1582.         printf("   threshold = %.17e .\n", UfThold);
  1583.         }
  1584.         else if (! (V9 > UfThold * (One + E9)))
  1585.             printf("This computed value is O.K.\n");
  1586.         else {
  1587.             BadCond(Defect, "this is not between 0 and underflow\n");
  1588.             printf("   threshold = %.17e .\n", UfThold);
  1589.             }
  1590.         }
  1591.     /*=============================================*/
  1592.     Milestone = 140;
  1593.     /*=============================================*/
  1594.     printf("\n");
  1595.     /* ...calculate Exp2 == exp(2) == 7.389056099... */
  1596.     X = Zero;
  1597.     I = 2;
  1598.     Y = Two * Three;
  1599.     Q = Zero;
  1600.     N = 0;
  1601.     do  {
  1602.         Z = X;
  1603.         I = I + 1;
  1604.         Y = Y / (I + I);
  1605.         R = Y + Q;
  1606.         X = Z + R;
  1607.         Q = (Z - X) + R;
  1608.         } while(X > Z);
  1609.     Z = (OneAndHalf + One / Eight) + X / (OneAndHalf * ThirtyTwo);
  1610.     X = Z * Z;
  1611.     Exp2 = X * X;
  1612.     X = F9;
  1613.     Y = X - U1;
  1614.     printf("Testing X^((X + 1) / (X - 1)) vs. exp(2) = %.17e as X -> 1.\n",
  1615.         Exp2);
  1616.     for(I = 1;;) {
  1617.         Z = X - BInvrse;
  1618.         Z = (X + One) / (Z - (One - BInvrse));
  1619.         Q = POW(X, Z) - Exp2;
  1620.         if (FABS(Q) > TwoForty * U2) {
  1621.             N = 1;
  1622.              V9 = (X - BInvrse) - (One - BInvrse);
  1623.             BadCond(Defect, "Calculated");
  1624.             printf(" %.17e for\n", POW(X,Z));
  1625.             printf("\t(1 + (%.17e) ^ (%.17e);\n", V9, Z);
  1626.             printf("\tdiffers from correct value by %.17e .\n", Q);
  1627.             printf("\tThis much error may spoil financial\n");
  1628.             printf("\tcalculations involving tiny interest rates.\n");
  1629.             break;
  1630.             }
  1631.         else {
  1632.             Z = (Y - X) * Two + Y;
  1633.             X = Y;
  1634.             Y = Z;
  1635.             Z = One + (X - F9)*(X - F9);
  1636.             if (Z > One && I < NoTrials) I++;
  1637.             else  {
  1638.                 if (X > One) {
  1639.                     if (N == 0)
  1640.                        printf("Accuracy seems adequate.\n");
  1641.                     break;
  1642.                     }
  1643.                 else {
  1644.                     X = One + U2;
  1645.                     Y = U2 + U2;
  1646.                     Y += X;
  1647.                     I = 1;
  1648.                     }
  1649.                 }
  1650.             }
  1651.         }
  1652.     /*=============================================*/
  1653.     Milestone = 150;
  1654.     /*=============================================*/
  1655.     printf("Testing powers Z^Q at four nearly extreme values.\n");
  1656.     N = 0;
  1657.     Z = A1;
  1658.     Q = FLOOR(Half - LOG(C) / LOG(A1));
  1659.     Break = False;
  1660.     do  {
  1661.         X = CInvrse;
  1662.         Y = POW(Z, Q);
  1663.         IsYeqX();
  1664.         Q = - Q;
  1665.         X = C;
  1666.         Y = POW(Z, Q);
  1667.         IsYeqX();
  1668.         if (Z < One) Break = True;
  1669.         else Z = AInvrse;
  1670.         } while ( ! (Break));
  1671.     PrintIfNPositive();
  1672.     if (N == 0) printf(" ... no discrepancies found.\n");
  1673.     printf("\n");
  1674.     
  1675.     /*=============================================*/
  1676.     Milestone = 160;
  1677.     /*=============================================*/
  1678.     Pause();
  1679.     printf("Searching for Overflow threshold:\n");
  1680.     printf("This may generate an error.\n");
  1681.     Y = - CInvrse;
  1682.     V9 = HInvrse * Y;
  1683.     sigsave = sigfpe;
  1684.     if (setjmp(ovfl_buf)) { I = 0; V9 = Y; goto overflow; }
  1685.     do {
  1686.         V = Y;
  1687.         Y = V9;
  1688.         V9 = HInvrse * Y;
  1689.         } while(V9 < Y);
  1690.     I = 1;
  1691. overflow:
  1692.     sigsave = 0;
  1693.     Z = V9;
  1694.     printf("Can `Z = -Y' overflow?\n");
  1695.     printf("Trying it on Y = %.17e .\n", Y);
  1696.     V9 = - Y;
  1697.     V0 = V9;
  1698.     if (V - Y == V + V0) printf("Seems O.K.\n");
  1699.     else {
  1700.         printf("finds a ");
  1701.         BadCond(Flaw, "-(-Y) differs from Y.\n");
  1702.         }
  1703.     if (Z != Y) {
  1704.         BadCond(Serious, "");
  1705.         printf("overflow past %.17e\n\tshrinks to %.17e .\n", Y, Z);
  1706.         }
  1707.     if (I) {
  1708.         Y = V * (HInvrse * U2 - HInvrse);
  1709.         Z = Y + ((One - HInvrse) * U2) * V;
  1710.         if (Z < V0) Y = Z;
  1711.         if (Y < V0) V = Y;
  1712.         if (V0 - V < V0) V = V0;
  1713.         }
  1714.     else {
  1715.         V = Y * (HInvrse * U2 - HInvrse);
  1716.         V = V + ((One - HInvrse) * U2) * Y;
  1717.         }
  1718.     printf("Overflow threshold is V  = %.17e .\n", V);
  1719.     if (I) printf("Overflow saturates at V0 = %.17e .\n", V0);
  1720.     else printf("There is no saturation value because \
  1721. the system traps on overflow.\n");
  1722.     V9 = V * One;
  1723.     printf("No Overflow should be signaled for V * 1 = %.17e\n", V9);
  1724.     V9 = V / One;
  1725.     printf("                           nor for V / 1 = %.17e .\n", V9);
  1726.     printf("Any overflow signal separating this * from the one\n");
  1727.     printf("above is a DEFECT.\n");
  1728.     /*=============================================*/
  1729.     Milestone = 170;
  1730.     /*=============================================*/
  1731.     if (!(-V < V && -V0 < V0 && -UfThold < V && UfThold < V)) {
  1732.         BadCond(Failure, "Comparisons involving ");
  1733.         printf("+-%g, +-%g\nand +-%g are confused by Overflow.",
  1734.             V, V0, UfThold);
  1735.         }
  1736.     /*=============================================*/
  1737.     Milestone = 175;
  1738.     /*=============================================*/
  1739.     printf("\n");
  1740.     for(Indx = 1; Indx <= 3; ++Indx) {
  1741.         switch (Indx)  {
  1742.             case 1: Z = UfThold; break;
  1743.             case 2: Z = E0; break;
  1744.             case 3: Z = PseudoZero; break;
  1745.             }
  1746.         if (Z != Zero) {
  1747.             V9 = SQRT(Z);
  1748.             Y = V9 * V9;
  1749.             if (Y / (One - Radix * E9) < Z
  1750.                || Y > (One + Radix * E9) * Z) { /* dgh: + E9 --> * E9 */
  1751.                 if (V9 > U1) BadCond(Serious, "");
  1752.                 else BadCond(Defect, "");
  1753.                 printf("Comparison alleges that what prints as Z = %.17e\n", Z);
  1754.                 printf(" is too far from sqrt(Z) ^ 2 = %.17e .\n", Y);
  1755.                 }
  1756.             }
  1757.         }
  1758.     /*=============================================*/
  1759.     Milestone = 180;
  1760.     /*=============================================*/
  1761.     for(Indx = 1; Indx <= 2; ++Indx) {
  1762.         if (Indx == 1) Z = V;
  1763.         else Z = V0;
  1764.         V9 = SQRT(Z);
  1765.         X = (One - Radix * E9) * V9;
  1766.         V9 = V9 * X;
  1767.         if (((V9 < (One - Two * Radix * E9) * Z) || (V9 > Z))) {
  1768.             Y = V9;
  1769.             if (X < W) BadCond(Serious, "");
  1770.             else BadCond(Defect, "");
  1771.             printf("Comparison alleges that Z = %17e\n", Z);
  1772.             printf(" is too far from sqrt(Z) ^ 2 (%.17e) .\n", Y);
  1773.             }
  1774.         }
  1775.     /*=============================================*/
  1776.     /*SPLIT
  1777.     }
  1778. #include "paranoia.h"
  1779. int part8(VOID){
  1780. */
  1781.     Milestone = 190;
  1782.     /*=============================================*/
  1783.     Pause();
  1784.     X = UfThold * V;
  1785.     Y = Radix * Radix;
  1786.     if (X*Y < One || X > Y) {
  1787.         if (X * Y < U1 || X > Y/U1) BadCond(Defect, "Badly");
  1788.         else BadCond(Flaw, "");
  1789.             
  1790.         printf(" unbalanced range; UfThold * V = %.17e\n\t%s\n",
  1791.             X, "is too far from 1.\n");
  1792.         }
  1793.     /*=============================================*/
  1794.     Milestone = 200;
  1795.     /*=============================================*/
  1796.     for (Indx = 1; Indx <= 5; ++Indx)  {
  1797.         X = F9;
  1798.         switch (Indx)  {
  1799.             case 2: X = One + U2; break;
  1800.             case 3: X = V; break;
  1801.             case 4: X = UfThold; break;
  1802.             case 5: X = Radix;
  1803.             }
  1804.         Y = X;
  1805.         sigsave = sigfpe;
  1806.         if (setjmp(ovfl_buf))
  1807.             printf("  X / X  traps when X = %g\n", X);
  1808.         else {
  1809.             V9 = (Y / X - Half) - Half;
  1810.             if (V9 == Zero) continue;
  1811.             if (V9 == - U1 && Indx < 5) BadCond(Flaw, "");
  1812.             else BadCond(Serious, "");
  1813.             printf("  X / X differs from 1 when X = %.17e\n", X);
  1814.             printf("  instead, X / X - 1/2 - 1/2 = %.17e .\n", V9);
  1815.             }
  1816.         sigsave = 0;
  1817.         }
  1818.     /*=============================================*/
  1819.     Milestone = 210;
  1820.     /*=============================================*/
  1821.     MyZero = Zero;
  1822.     printf("\n");
  1823.     printf("What message and/or values does Division by Zero produce?\n") ;
  1824. #ifndef NOPAUSE
  1825.     printf("This can interupt your program.  You can ");
  1826.     printf("skip this part if you wish.\n");
  1827.     printf("Do you wish to compute 1 / 0? ");
  1828.     fflush(stdout);
  1829.     read (KEYBOARD, ch, 8);
  1830.     if ((ch[0] == 'Y') || (ch[0] == 'y')) {
  1831. #endif
  1832.         sigsave = sigfpe;
  1833.         printf("    Trying to compute 1 / 0 produces ...");
  1834.         if (!setjmp(ovfl_buf)) printf("  %.7e .\n", One / MyZero);
  1835.         sigsave = 0;
  1836. #ifndef NOPAUSE
  1837.         }
  1838.     else printf("O.K.\n");
  1839.     printf("\nDo you wish to compute 0 / 0? ");
  1840.     fflush(stdout);
  1841.     read (KEYBOARD, ch, 80);
  1842.     if ((ch[0] == 'Y') || (ch[0] == 'y')) {
  1843. #endif
  1844.         sigsave = sigfpe;
  1845.         printf("\n    Trying to compute 0 / 0 produces ...");
  1846.         if (!setjmp(ovfl_buf)) printf("  %.7e .\n", Zero / MyZero);
  1847.         sigsave = 0;
  1848. #ifndef NOPAUSE
  1849.         }
  1850.     else printf("O.K.\n");
  1851. #endif
  1852.     /*=============================================*/
  1853.     Milestone = 220;
  1854.     /*=============================================*/
  1855.     Pause();
  1856.     printf("\n");
  1857.     {
  1858.         static char *msg[] = {
  1859.             "FAILUREs  encountered =",
  1860.             "SERIOUS DEFECTs  discovered =",
  1861.             "DEFECTs  discovered =",
  1862.             "FLAWs  discovered =" };
  1863.         int i;
  1864.         for(i = 0; i < 4; i++) if (ErrCnt[i])
  1865.             printf("The number of  %-29s %d.\n",
  1866.                 msg[i], ErrCnt[i]);
  1867.         }
  1868.     printf("\n");
  1869.     if ((ErrCnt[Failure] + ErrCnt[Serious] + ErrCnt[Defect]
  1870.             + ErrCnt[Flaw]) > 0) {
  1871.         if ((ErrCnt[Failure] + ErrCnt[Serious] + ErrCnt[
  1872.             Defect] == 0) && (ErrCnt[Flaw] > 0)) {
  1873.             printf("The arithmetic diagnosed seems ");
  1874.             printf("Satisfactory though flawed.\n");
  1875.             }
  1876.         if ((ErrCnt[Failure] + ErrCnt[Serious] == 0)
  1877.             && ( ErrCnt[Defect] > 0)) {
  1878.             printf("The arithmetic diagnosed may be Acceptable\n");
  1879.             printf("despite inconvenient Defects.\n");
  1880.             }
  1881.         if ((ErrCnt[Failure] + ErrCnt[Serious]) > 0) {
  1882.             printf("The arithmetic diagnosed has ");
  1883.             printf("unacceptable Serious Defects.\n");
  1884.             }
  1885.         if (ErrCnt[Failure] > 0) {
  1886.             printf("Potentially fatal FAILURE may have spoiled this");
  1887.             printf(" program's subsequent diagnoses.\n");
  1888.             }
  1889.         }
  1890.     else {
  1891.         printf("No failures, defects nor flaws have been discovered.\n");
  1892.         if (! ((RMult == Rounded) && (RDiv == Rounded)
  1893.             && (RAddSub == Rounded) && (RSqrt == Rounded))) 
  1894.             printf("The arithmetic diagnosed seems Satisfactory.\n");
  1895.         else {
  1896.             if (StickyBit >= One &&
  1897.                 (Radix - Two) * (Radix - Nine - One) == Zero) {
  1898.                 printf("Rounding appears to conform to ");
  1899.                 printf("the proposed IEEE standard P");
  1900.                 if ((Radix == Two) &&
  1901.                      ((Precision - Four * Three * Two) *
  1902.                       ( Precision - TwentySeven -
  1903.                        TwentySeven + One) == Zero)) 
  1904.                     printf("754");
  1905.                 else printf("854");
  1906.                 if (IEEE) printf(".\n");
  1907.                 else {
  1908.                     printf(",\nexcept for possibly Double Rounding");
  1909.                     printf(" during Gradual Underflow.\n");
  1910.                     }
  1911.                 }
  1912.             printf("The arithmetic diagnosed appears to be Excellent!\n");
  1913.             }
  1914.         }
  1915.     if (fpecount)
  1916.         printf("\nA total of %d floating point exceptions were registered.\n",
  1917.             fpecount);
  1918.     printf("END OF TEST.\n");
  1919.     return 0;
  1920.     }
  1921.  
  1922. /*SPLIT subs.c
  1923. #include "paranoia.h"
  1924. */
  1925.  
  1926.  FLOAT
  1927. Sign (FP X)
  1928. #ifdef KR_headers
  1929. FLOAT X;
  1930. #endif
  1931. { return X >= 0. ? 1.0 : -1.0; }
  1932.  
  1933.  void
  1934. Pause(VOID)
  1935. {
  1936. #ifndef NOPAUSE
  1937.     char ch[8];
  1938.  
  1939.     printf("\nTo continue, press RETURN");
  1940.     fflush(stdout);
  1941.     read(KEYBOARD, ch, 8);
  1942. #endif
  1943.     printf("\nDiagnosis resumes after milestone Number %d", Milestone);
  1944.     printf("          Page: %d\n\n", PageNo);
  1945.     ++Milestone;
  1946.     ++PageNo;
  1947.     }
  1948.  
  1949.  void
  1950. TstCond (INT K, INT Valid, CHARP T)
  1951. #ifdef KR_headers
  1952. int K, Valid;
  1953. char *T;
  1954. #endif
  1955. { if (! Valid) { BadCond(K,T); printf(".\n"); } }
  1956.  
  1957.  void
  1958. BadCond(INT K, CHARP T)
  1959. #ifdef KR_headers
  1960. int K;
  1961. char *T;
  1962. #endif
  1963. {
  1964.     static char *msg[] = { "FAILURE", "SERIOUS DEFECT", "DEFECT", "FLAW" };
  1965.  
  1966.     ErrCnt [K] = ErrCnt [K] + 1;
  1967.     printf("%s:  %s", msg[K], T);
  1968.     }
  1969.  
  1970.  
  1971.  FLOAT
  1972. Random(VOID)
  1973. /*  Random computes
  1974.      X = (Random1 + Random9)^5
  1975.      Random1 = X - FLOOR(X) + 0.000005 * X;
  1976.    and returns the new value of Random1
  1977. */
  1978. {
  1979.     FLOAT X, Y;
  1980.     
  1981.     X = Random1 + Random9;
  1982.     Y = X * X;
  1983.     Y = Y * Y;
  1984.     X = X * Y;
  1985.     Y = X - FLOOR(X);
  1986.     Random1 = Y + X * 0.000005;
  1987.     return(Random1);
  1988.     }
  1989.  
  1990.  void
  1991. SqXMinX (INT ErrKind)
  1992. #ifdef KR_headers
  1993. int ErrKind;
  1994. #endif
  1995. {
  1996.     FLOAT XA, XB;
  1997.     
  1998.     XB = X * BInvrse;
  1999.     XA = X - XB;
  2000.     SqEr = ((SQRT(X * X) - XB) - XA) / OneUlp;
  2001.     if (SqEr != Zero) {
  2002.         if (SqEr < MinSqEr) MinSqEr = SqEr;
  2003.         if (SqEr > MaxSqEr) MaxSqEr = SqEr;
  2004.         J = J + 1.0;
  2005.         BadCond(ErrKind, "\n");
  2006.         printf("sqrt( %.17e) - %.17e  = %.17e\n", X * X, X, OneUlp * SqEr);
  2007.         printf("\tinstead of correct value 0 .\n");
  2008.         }
  2009.     }
  2010.  
  2011.  void
  2012. NewD(VOID)
  2013. {
  2014.     X = Z1 * Q;
  2015.     X = FLOOR(Half - X / Radix) * Radix + X;
  2016.     Q = (Q - X * Z) / Radix + X * X * (D / Radix);
  2017.     Z = Z - Two * X * D;
  2018.     if (Z <= Zero) {
  2019.         Z = - Z;
  2020.         Z1 = - Z1;
  2021.         }
  2022.     D = Radix * D;
  2023.     }
  2024.  
  2025.  void
  2026. SR3750(VOID)
  2027. {
  2028.     if (! ((X - Radix < Z2 - Radix) || (X - Z2 > W - Z2))) {
  2029.         I = I + 1;
  2030.         X2 = SQRT(X * D);
  2031.         Y2 = (X2 - Z2) - (Y - Z2);
  2032.         X2 = X8 / (Y - Half);
  2033.         X2 = X2 - Half * X2 * X2;
  2034.         SqEr = (Y2 + Half) + (Half - X2);
  2035.         if (SqEr < MinSqEr) MinSqEr = SqEr;
  2036.         SqEr = Y2 - X2;
  2037.         if (SqEr > MaxSqEr) MaxSqEr = SqEr;
  2038.         }
  2039.     }
  2040.  
  2041.  void
  2042. IsYeqX(VOID)
  2043. {
  2044.     if (Y != X) {
  2045.         if (N <= 0) {
  2046.             if (Z == Zero && Q <= Zero)
  2047.                 printf("WARNING:  computing\n");
  2048.             else BadCond(Defect, "computing\n");
  2049.             printf("\t(%.17e) ^ (%.17e)\n", Z, Q);
  2050.             printf("\tyielded %.17e;\n", Y);
  2051.             printf("\twhich compared unequal to correct %.17e ;\n",
  2052.                 X);
  2053.             printf("\t\tthey differ by %.17e .\n", Y - X);
  2054.             }
  2055.         N = N + 1; /* ... count discrepancies. */
  2056.         }
  2057.     }
  2058.  
  2059.  void
  2060. SR3980(VOID)
  2061. {
  2062.     do {
  2063.         Q = (FLOAT) I;
  2064.         Y = POW(Z, Q);
  2065.         IsYeqX();
  2066.         if (++I > M) break;
  2067.         X = Z * X;
  2068.         } while ( X < W );
  2069.     }
  2070.  
  2071.  void
  2072. PrintIfNPositive(VOID)
  2073. {
  2074.     if (N > 0) printf("Similar discrepancies have occurred %d times.\n", N);
  2075.     }
  2076.  
  2077.  void
  2078. TstPtUf(VOID)
  2079. {
  2080.     N = 0;
  2081.     if (Z != Zero) {
  2082.         printf("Since comparison denies Z = 0, evaluating ");
  2083.         printf("(Z + Z) / Z should be safe.\n");
  2084.         sigsave = sigfpe;
  2085.         if (setjmp(ovfl_buf)) goto very_serious;
  2086.         Q9 = (Z + Z) / Z;
  2087.         printf("What the machine gets for (Z + Z) / Z is  %.17e .\n",
  2088.             Q9);
  2089.         if (FABS(Q9 - Two) < Radix * U2) {
  2090.             printf("This is O.K., provided Over/Underflow");
  2091.             printf(" has NOT just been signaled.\n");
  2092.             }
  2093.         else {
  2094.             if ((Q9 < One) || (Q9 > Two)) {
  2095. very_serious:
  2096.                 N = 1;
  2097.                 ErrCnt [Serious] = ErrCnt [Serious] + 1;
  2098.                 printf("This is a VERY SERIOUS DEFECT!\n");
  2099.                 }
  2100.             else {
  2101.                 N = 1;
  2102.                 ErrCnt [Defect] = ErrCnt [Defect] + 1;
  2103.                 printf("This is a DEFECT!\n");
  2104.                 }
  2105.             }
  2106.         sigsave = 0;
  2107.         V9 = Z * One;
  2108.         Random1 = V9;
  2109.         V9 = One * Z;
  2110.         Random2 = V9;
  2111.         V9 = Z / One;
  2112.         if ((Z == Random1) && (Z == Random2) && (Z == V9)) {
  2113.             if (N > 0) Pause();
  2114.             }
  2115.         else {
  2116.             N = 1;
  2117.             BadCond(Defect, "What prints as Z = ");
  2118.             printf("%.17e\n\tcompares different from  ", Z);
  2119.             if (Z != Random1) printf("Z * 1 = %.17e ", Random1);
  2120.             if (! ((Z == Random2)
  2121.                 || (Random2 == Random1)))
  2122.                 printf("1 * Z == %g\n", Random2);
  2123.             if (! (Z == V9)) printf("Z / 1 = %.17e\n", V9);
  2124.             if (Random2 != Random1) {
  2125.                 ErrCnt [Defect] = ErrCnt [Defect] + 1;
  2126.                 BadCond(Defect, "Multiplication does not commute!\n");
  2127.                 printf("\tComparison alleges that 1 * Z = %.17e\n",
  2128.                     Random2);
  2129.                 printf("\tdiffers from Z * 1 = %.17e\n", Random1);
  2130.                 }
  2131.             Pause();
  2132.             }
  2133.         }
  2134.     }
  2135.  
  2136.  void
  2137. notify(CHARP s)
  2138. #ifdef KR_headers
  2139.  char *s;
  2140. #endif
  2141. {
  2142.     printf("%s test appears to be inconsistent...\n", s);
  2143.     printf("   PLEASE NOTIFY KARPINKSI!\n");
  2144.     }
  2145.  
  2146. /*SPLIT msgs.c
  2147. #include "paranoia.h"
  2148. */
  2149.  
  2150.  void
  2151. msglist(CHARPP s)
  2152. #ifdef KR_headers
  2153. char **s;
  2154. #endif
  2155. { while(*s) printf("%s\n", *s++); }
  2156.  
  2157.  void
  2158. Instructions(VOID)
  2159. {
  2160.   static char *instr[] = {
  2161.     "Lest this program stop prematurely, i.e. before displaying\n",
  2162.     "    `END OF TEST',\n",
  2163.     "try to persuade the computer NOT to terminate execution when an",
  2164.     "error like Over/Underflow or Division by Zero occurs, but rather",
  2165.     "to persevere with a surrogate value after, perhaps, displaying some",
  2166.     "warning.  If persuasion avails naught, don't despair but run this",
  2167.     "program anyway to see how many milestones it passes, and then",
  2168.     "amend it to make further progress.\n",
  2169.     "Answer questions with Y, y, N or n (unless otherwise indicated).\n",
  2170.     0};
  2171.  
  2172.     msglist(instr);
  2173.     }
  2174.  
  2175.  void
  2176. Heading(VOID)
  2177. {
  2178.   static char *head[] = {
  2179.     "Users are invited to help debug and augment this program so it will",
  2180.     "cope with unanticipated and newly uncovered arithmetic pathologies.\n",
  2181.     "Please send suggestions and interesting results to",
  2182.     "\tRichard Karpinski",
  2183.     "\tComputer Center U-76",
  2184.     "\tUniversity of California",
  2185.     "\tSan Francisco, CA 94143-0704, USA\n",
  2186.     "In doing so, please include the following information:",
  2187. #ifdef Single
  2188.     "\tPrecision:\tsingle;",
  2189. #else
  2190.     "\tPrecision:\tdouble;",
  2191. #endif
  2192.     "\tVersion:\t10 February 1989;",
  2193.     "\tComputer:\n",
  2194.     "\tCompiler:\n",
  2195.     "\tOptimization level:\n",
  2196.     "\tOther relevant compiler options:",
  2197.     0};
  2198.  
  2199.     msglist(head);
  2200.     }
  2201.  
  2202.  void
  2203. Characteristics(VOID)
  2204. {
  2205.     static char *chars[] = {
  2206.      "Running this program should reveal these characteristics:",
  2207.     "     Radix = 1, 2, 4, 8, 10, 16, 100, 256 ...",
  2208.     "     Precision = number of significant digits carried.",
  2209.     "     U2 = Radix/Radix^Precision = One Ulp",
  2210.     "\t(OneUlpnit in the Last Place) of 1.000xxx .",
  2211.     "     U1 = 1/Radix^Precision = One Ulp of numbers a little less than 1.0 .",
  2212.     "     Adequacy of guard digits for Mult., Div. and Subt.",
  2213.     "     Whether arithmetic is chopped, correctly rounded, or something else",
  2214.     "\tfor Mult., Div., Add/Subt. and Sqrt.",
  2215.     "     Whether a Sticky Bit used correctly for rounding.",
  2216.     "     UnderflowThreshold = an underflow threshold.",
  2217.     "     E0 and PseudoZero tell whether underflow is abrupt, gradual, or fuzzy.",
  2218.     "     V = an overflow threshold, roughly.",
  2219.     "     V0  tells, roughly, whether  Infinity  is represented.",
  2220.     "     Comparisions are checked for consistency with subtraction",
  2221.     "\tand for contamination with pseudo-zeros.",
  2222.     "     Sqrt is tested.  Y^X is not tested.",
  2223.     "     Extra-precise subexpressions are revealed but NOT YET tested.",
  2224.     "     Decimal-Binary conversion is NOT YET tested for accuracy.",
  2225.     0};
  2226.  
  2227.     msglist(chars);
  2228.     }
  2229.  
  2230.  void
  2231. History(VOID)
  2232. { /* History */
  2233.  /* Converted from Brian Wichmann's Pascal version to C by Thos Sumner,
  2234.     with further massaging by David M. Gay. */
  2235.  
  2236.   static char *hist[] = {
  2237.     "The program attempts to discriminate among",
  2238.     "   FLAWs, like lack of a sticky bit,",
  2239.     "   Serious DEFECTs, like lack of a guard digit, and",
  2240.     "   FAILUREs, like 2+2 == 5 .",
  2241.     "Failures may confound subsequent diagnoses.\n",
  2242.     "The diagnostic capabilities of this program go beyond an earlier",
  2243.     "program called `MACHAR', which can be found at the end of the",
  2244.     "book  `Software Manual for the Elementary Functions' (1980) by",
  2245.     "W. J. Cody and W. Waite. Although both programs try to discover",
  2246.     "the Radix, Precision and range (over/underflow thresholds)",
  2247.     "of the arithmetic, this program tries to cope with a wider variety",
  2248.     "of pathologies, and to say how well the arithmetic is implemented.",
  2249.     "\nThe program is based upon a conventional radix representation for",
  2250.     "floating-point numbers, but also allows logarithmic encoding",
  2251.     "as used by certain early WANG machines.\n",
  2252.     "BASIC version of this program (C) 1983 by Prof. W. M. Kahan;",
  2253.     "see source comments for more history.",
  2254.     0};
  2255.  
  2256.     msglist(hist);
  2257.     }
  2258.